/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package imat;

import java.awt.CardLayout;
import se.chalmers.ait.dat215.project.CreditCard;
import se.chalmers.ait.dat215.project.Customer;

/**
 *
 * @author sunding
 */
public class InfoPresenter {
    private InfoView infoView;
    private Customer customer;
    private CreditCard creditCard;

    public InfoPresenter (InfoView view) {
        infoView = view;
        customer = DataHandler.getInstance().getCustomer();
        creditCard = DataHandler.getInstance().getCreditCard();
    }

    public void saveInfo() {
        customer.setFirstName(infoView.getFirstName());
        customer.setLastName(infoView.getLastName());
        customer.setAddress(infoView.getAddress());
        customer.setPostCode(infoView.getPostCode());
        customer.setPostAddress(infoView.getPostAddress());
        customer.setPhoneNumber(infoView.getPhoneNumber());

        creditCard.setCardNumber(infoView.getCardNumber());
        creditCard.setValidMonth(infoView.getValidMonth());
        creditCard.setValidYear(infoView.getValidYear());
        creditCard.setVerificationCode(infoView.getVerificationCode());

        infoView.firstNameError.setVisible(false);
        infoView.lastNameError.setVisible(false);
        infoView.addressError.setVisible(false);
        infoView.postCodeError.setVisible(false);
        infoView.postAddressError.setVisible(false);
        infoView.phoneNumberError.setVisible(false);
        infoView.payError.setVisible(false);
        infoView.saveLabel.setVisible(false);

        errorCheck();
        if (DataHandler.getInstance().isCustomerComplete() && isPayInfoComplete()) {
            infoView.saveLabel.setVisible(true);
            infoView.clearColors();
        }
    }

    public void errorCheck() {
        if (!DataHandler.getInstance().isCustomerComplete()) {
            if (infoView.getFirstName().equals("")) {
                infoView.firstNameError.setVisible(true);
            }
            if (infoView.getLastName().equals("")) {
                infoView.lastNameError.setVisible(true);
            }
            if (infoView.getAddress().equals("")) {
                infoView.addressError.setVisible(true);
            }
            if (infoView.getPostCode().equals("")) {
                infoView.postCodeError.setVisible(true);
            }
            if (infoView.getPostAddress().equals("")) {
                infoView.postAddressError.setVisible(true);
            } if (infoView.getPhoneNumber().equals("")) {
                infoView.phoneNumberError.setVisible(true);
            }
        }
        if (!isPayInfoComplete()) {
            infoView.payError.setVisible(true);
        }
    }

    public void paymentMethodChanged(int paymentMethod) {
        if (paymentMethod == 0) {
            CardLayout cl = (CardLayout)(infoView.getPaymentPanel().getLayout());
            cl.show(infoView.getPaymentPanel(), "defaultCard");
        }
        else if (paymentMethod == 1) {
            CardLayout cl = (CardLayout)(infoView.getPaymentPanel().getLayout());
            cl.show(infoView.getPaymentPanel(), "creditCardCard");
        }
    }

    public boolean isPayInfoComplete() {
        if (infoView.getCardNumber().length() < 16) {
            return false;
        } else if (infoView.getValidMonth() > 12 || infoView.getValidMonth() < 1) {
            return false;
        } else if (infoView.getValidYear() < 1950 || infoView.getVerificationCode() > 3000) {
            return false;
        } else if (infoView.getVerificationCode() < 99 || infoView.getVerificationCode() > 999) {
            return false;
        } else 
            return true;
    }
}
